/*
 * Copyright 2017 JessYan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.cj.mobile.iabp.mvp.ui.holder;

import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ConvertUtils;
import com.cj.mobile.iabp.R;
import com.cj.mobile.iabp.app.global.AccountManager;
import com.cj.mobile.iabp.app.utils.CommonUtils;
import com.cj.mobile.iabp.mvp.model.entity.RoundsInfo;
import com.jess.arms.base.BaseHolder;
import com.jess.arms.base.DefaultAdapter;

import butterknife.BindView;

/**
 * ================================================
 * 展示 {@link BaseHolder} 的用法
 * <p>
 * Created by JessYan on 9/4/16 12:56
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * ================================================
 */
public class RoundsItemHolder extends BaseHolder<RoundsInfo> {
    @BindView(R.id.rounds_item_layout)
    LinearLayout mLayout;

    @BindView(R.id.tv_install_date)
    TextView txviInstallDate;
    @BindView(R.id.tv_name)
    TextView txviName;

    @BindView(R.id.tv_rounds_select)
    TextView txviSelect;

    @BindView(R.id.tv_rounds_update)
    TextView txviUpdate;

    @BindView(R.id.tv_rounds_del)
    TextView txviDel;

    private AccountManager mAccountManager;

    public RoundsItemHolder(View itemView) {
        super(itemView);
        mAccountManager = new AccountManager(itemView.getContext());
    }

    @Override
    public void setData(@NonNull RoundsInfo info, int position) {
        // 隔行设置不同背景色
        if (position % 2 == 0) {
            mLayout.setBackgroundColor(mLayout.getContext().getResources().getColor(android.R.color.white));
        } else {
            mLayout.setBackgroundColor(mLayout.getContext().getResources().getColor(R.color.form_color));
        }

        if (info.getRoundTime() != null && info.getRoundTime().length() > 0) {
            txviInstallDate.setText(info.getRoundTime().split(" ")[0]);
        } else {
            txviInstallDate.setText("");
        }

        txviName.setText(CommonUtils.isEmptyReturnStr(info.getDoctorName()));

        if (!TextUtils.isEmpty(info.getPer())) {
            if (info.getPer().equals("1")) {
                txviUpdate.setText("修改");
                txviUpdate.setTextColor(txviUpdate.getContext().getResources().getColor(R.color.text_h_bg_color));
            } else {
                txviUpdate.setText("暂存");
                txviUpdate.setTextColor(txviUpdate.getContext().getResources().getColor(R.color.text_l_bg_color));
            }
        }

        this.txviDel.setVisibility(mAccountManager.getOptPermissions() ? View.VISIBLE : View.GONE);

        this.txviSelect.setOnClickListener(this);
        this.txviUpdate.setOnClickListener(this);
        this.txviDel.setOnClickListener(this);
    }

    /**
     * 动态设置Margin
     */
    private void setLayoutMargin(boolean isSet) {
        ConstraintLayout.LayoutParams layoutParam = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
        if (isSet)
            layoutParam.setMargins(0, ConvertUtils.dp2px(10), 0, 0);
        else
            layoutParam.setMargins(0, 0, 0, 0);

        mLayout.setLayoutParams(layoutParam);
    }

    /**
     * 在 Activity 的 onDestroy 中使用 {@link DefaultAdapter#releaseAllHolder(RecyclerView)} 方法 (super.onDestroy() 之前)
     * {@link BaseHolder#onRelease()} 才会被调用, 可以在此方法中释放一些资源
     */
    @Override
    protected void onRelease() {
        this.mLayout = null;

        this.mAccountManager = null;

        this.txviInstallDate = null;
        this.txviName = null;
        this.txviSelect = null;
        this.txviUpdate = null;
        this.txviDel = null;
    }
}
